package com.moyun.finance.service;

import com.moyun.finance.entity.OrgUser;
import com.moyun.finance.entity.Projects;
import com.moyun.finance.entity.Response;
import com.moyun.finance.repository.OrgUserRepository;
import com.moyun.finance.repository.ProjectsRepository;
import com.moyun.finance.utils.BaseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProjectsService {
    @Autowired
    ProjectsRepository pr;
    @Autowired
    OrgUserRepository our;

    public Response save(Projects projects){
        projects.setId(BaseUtil.uuid());
        pr.save(projects);
        return new Response("添加成功!",true);
    }

    public Response update(Projects projects){
        pr.save(projects);
        return new Response("项目信息修改成功!",true);
    }

    public Response delete(Projects projects){
        pr.delete(projects);
        return new Response("项目删除成功",true);
    }

    //本公司所有项目
    public List<Projects> findAll(String companyId){
        List<Projects> pros =  pr.findByCompanyId(companyId);
        for(Projects pro : pros){
            OrgUser user = our.findById(pro.getLeadingId()).orElse(null);
            pro.setUser(user);
        }
        return pros;
    }

    public Projects findOneById(String projectsId){
        Projects pro = pr.findById(projectsId).orElse(null);
        OrgUser user = our.findById(pro.getLeadingId()).orElse(null);
        pro.setUser(user);
        return pro;
    }

    public List<Projects> findByNameLike(String companyId, String projectsName){
        List<Projects> pros = pr.findByCompanyIdAndProjectNameLike(companyId, "%"+projectsName+"%");
        for(Projects pro : pros){
            OrgUser user = our.findById(pro.getLeadingId()).orElse(null);
            pro.setUser(user);
        }
        return pros;
    }

    public List<Projects> findByAllType(String companyId, String allTypeId){
        List<Projects> pros =  pr.findByCompanyIdAndAllTypeId(companyId, allTypeId);
        for(Projects pro : pros){
            OrgUser user = our.findById(pro.getLeadingId()).orElse(null);
            pro.setUser(user);
        }
        return pros;
    }
}
